/**
 * Tools AL工具
 * 创建时间：2023-3-6
 * 创建人：屑阿狼
 */

const md5 = require('blueimp-md5')
const uuid = require('node-uuid');

const Tools = {}

/**
 * RandomCode 生成4或8位字符的随机数
 * @param {Number} MaxNum Max Length to 4 or 8 (Default：4)
 */

Tools.RandomCode = (MaxNum) => {
    return new Promise((resolve, reject) => {
        var code = null
        if (MaxNum == null) {
            MaxNum = 4
        }
        if (MaxNum == 4) {
            code = Math.floor(Math.random() * 10000)
        } else if (MaxNum == 8) {
            code = Math.floor(Math.random() * 100000000)
        } else {
            reject("RandomCode => MaxNum - error")
        }
        resolve(code)
    })
}

/**
 * RandomAuth 生成随机唯一AuthToken
 */

Tools.RandomAuth = () => {
    return new Promise((resolve, reject) => {
        let abc = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
        let ABC = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        let ABCtwo = ABC
        let abctwo = abc
        let [max, min] = [Math.floor(Math.random() * (10 - 7 + 1) + 1), Math.floor(Math.random() * (17 - 10 + 1) + 17)];
        let Num = Math.round(Math.random() * 100000 + 100);
        let Numb = Math.round(Math.random() * 1000000);
        let aDate = new Date().getTime()
        let sDate = String(aDate)
        let cData = sDate.substring(8, 13) + ''
        abc = abc.sort(() => 0.4 - Math.random()).slice(max, min).slice(0, 8).join("");
        ABC = ABC.sort(() => 0.4 - Math.random()).slice(max, min).slice(0, 6).join("");
        abctwo = abctwo.sort(() => 0.4 - Math.random()).slice(max, min).slice(0, 2).join("");
        ABCtwo = ABCtwo.sort(() => 0.4 - Math.random()).slice(max, min).slice(0, 2).join("");
        var code = ABC + Numb + abctwo + ABCtwo + Num + abc + cData + Num + ABC + abc;
        resolve(code)
    })
}

/**
 * TimeMap 获取时间戳
 * @param {Number}
 */

Tools.TimeMap = () => {
    return new Promise((resolve, reject) => {
        var aDate = new Date().getTime()
        var timemap = String(aDate)
        resolve(timemap)
    })
}

/**
 * DataTime 已格式化的实时时间
 */

Tools.DataTime = () => {
    return new Promise((resolve, reject) => {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var dates = date.getDate();
        var h = date.getHours();
        var m = date.getMinutes();
        var s = date.getSeconds();
        m = checkTime(m);
        s = checkTime(s);

        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
        var time = h + ":" + m + ":" + s
        var datatime = year + "-" + month + "-" + dates + " " + time
        resolve(datatime)
    })
}

/**
 * Date 已格式化的实时时间
 */

Tools.Date = () => {
    return new Promise((resolve, reject) => {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var dates = date.getDate();

        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
        var dater = year + "-" + month + "-" + dates
        resolve(dater)
    })
}

/**
 * Md5加密
 * @param {Number} values
 */

Tools.Md5 = (values) => {
    return new Promise((resolve, reject) => {
        if (values == null) {
            reject("Md5 => values - error")
        } else {
            var Md5 = md5(values)
            resolve(Md5)
        }
    })
}

/**
 * UUID基于时间戳生成
 */

Tools.uuidv1 = () => {
    return new Promise((resolve, reject) => {
        resolve(uuid.v1())
    })
}

/**
 * UUID随机生成
 */

Tools.uuidv4 = () => {
    return new Promise((resolve, reject) => {
        resolve(uuid.v4())
    })
}

/**
 * UUID唯一ID
 * @param {String} ip
 */

Tools.useruuid = (ip) => {
    return new Promise((resolve, reject) => {
        if (ip != "") {
            resolve(uuid.v4() + "-" + md5(ip))
        } else {
            reject(ip + "- error")
        }
    })
}

export default Tools